package top.linyuxb.lycloud.rpc.starter.config;

import cn.hutool.extra.spring.EnableSpringUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import top.linyuxb.lycloud.rpc.starter.common.LyRpcBeanPostProcessor;
import top.linyuxb.lycloud.rpc.starter.common.LyRpcBeanUtil;
import top.linyuxb.lycloud.rpc.starter.controller.LyRpcClientController;
import top.linyuxb.lycloud.rpc.starter.properties.LyRpcClientProperties;

import java.util.Map;

/**
 * Created by Intellij IDEA.
 *
 * @Author: linyuxb
 * @Date: 2024/5/31 16:51
 * @Desc:
 */
@Slf4j
@Configuration
@EnableSpringUtil
@EnableConfigurationProperties(LyRpcClientProperties.class)
@Import(LyRpcClientController.class)
@ConditionalOnProperty(
        prefix = "ly-rpc-client",
        name = "open",
        havingValue = "true"
)
public class LyRpcClientConfig {
    @Resource
    private LyRpcClientProperties lyRpcClientProperties;

    @Bean(value = "lyRpcClientBeanPostProcessor")
    public BeanPostProcessor beanPostProcessor() {
        Map<String, Object> servers = LyRpcBeanUtil.getClientBeanMap(lyRpcClientProperties);
        return new LyRpcBeanPostProcessor(servers);
    }
}
